//
//  CityTableViewController.m
//  09-UITableViewHomeWork
//
//  Created by 曹曹 on 16/7/26.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "CityTableViewController.h"
#import "City.h"
#import "TownTableViewController.h"

#define kTableViewResuse @"reuseIdentifier"

@interface CityTableViewController ()

@end

@implementation CityTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TownTableViewController *townVC = [[TownTableViewController alloc] init];
    City *city = [_cityArray objectAtIndex:indexPath.row];
    townVC.townArray = [NSArray arrayWithArray:city.array];
    NSArray *tempArray = [city.name componentsSeparatedByString:@" "];
    townVC.title = tempArray[2];
    [self.navigationController pushViewController:townVC animated:YES];
}
#pragma mark - Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _cityArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewResuse];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kTableViewResuse];
    }
    City *city = [_cityArray objectAtIndex:indexPath.row];
    NSArray *tempArray = [city.name componentsSeparatedByString:@" "];
    cell.textLabel.text = tempArray[2];
    cell.detailTextLabel.text = tempArray.lastObject;
    return cell;
}
@end
